﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Ria;
using System.Windows.Ria.ApplicationServices;
using System.Windows.Ria.Data;
using System.Windows.Data;
using System.ComponentModel;
using System.Windows.Threading;
using AlexApp;

namespace ExpenseReport
{
    public partial class ExpenseReportSummary : Page
    {
        public ExpenseReportSummary()
        {
            InitializeComponent();

            ddsYourExpenseReports.SubmittedChanges += new EventHandler<SubmittedChangesEventArgs>(ddsYourExpenseReports_SubmittedChanges);
            ddsApprovalExpenseReports.SubmittedChanges += new EventHandler<SubmittedChangesEventArgs>(ddsYourExpenseReports_SubmittedChanges);
            ProcessLogin();
        }

        /// <summary>
        /// Method is required to allow refreshing of DomainDataSource after an Submit\Approve\Reject has 
        /// been submitted.
        /// </summary>
        /// <param name="sender">DomainDataSource that submitted changes</param>
        /// <param name="e">SubmittedChangesEventArgs returned by event</param>
        private void ddsYourExpenseReports_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
        {
            ProcessLogin();
        }

        /// <summary>
        /// Validates that the current user is authenticated and then explictly loads the correct 
        /// datasources based on the users role.
        /// </summary>
        private void ProcessLogin()
        {
            User currentUser = UserService.Current.User as User;
            if (currentUser != null && currentUser.IsAuthenticated) 
            {
                if (currentUser.Roles.Contains("employee"))
                {
                    ddsYourExpenseReports.Load();
                    yourExpenseReportPanel.Visibility = Visibility.Visible;
                    ddsYourExpenseReports.AutoLoad = true;
                }
                if (currentUser.Roles.Contains("manager"))
                {
                    ddsApprovalExpenseReports.Load();
                    approvalExpenseReportPanel.Visibility = Visibility.Visible;
                    ddsApprovalExpenseReports.AutoLoad = true;
                }
            }
            else
            {
                yourExpenseReportPanel.Visibility = Visibility.Collapsed;
                approvalExpenseReportPanel.Visibility = Visibility.Collapsed;
                ddsYourExpenseReports.AutoLoad = false;
                ddsApprovalExpenseReports.AutoLoad = false;
            }
        }

        /// <summary>
        /// Navigates to UpdateExpenseReport page with a zero ID so that a new expense report
        /// details is created.
        /// </summary>
        /// <param name="sender">Button initiating event.</param>
        /// <param name="e">RoutedEventArgs for button click</param>
        private void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            String uriString = "/UpdateExpenseReport.xaml?ExpenseReportId=" + "0";
            this.NavigationService.Navigate(new Uri(uriString, UriKind.Relative));
        }

        /// <summary>
        /// Navigates to UpdateExpenseReport page with a ID of selected expense report 
        /// so that details of expense report are shown on new page.
        /// </summary>
        /// <param name="sender">Button initiating event.</param>
        /// <param name="e">RoutedEventArgs for button click</param>
        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            if (dgYourExpenseReport.SelectedIndex >= 0)
            {
                String uriString = "/UpdateExpenseReport.xaml?ExpenseReportId=" + ((ExpenseReport)dgYourExpenseReport.SelectedItem).id;
                this.NavigationService.Navigate(new Uri(uriString, UriKind.Relative));
            }
        }

        /// <summary>
        /// Submits the expense report and saves the changes to the database.
        /// </summary>
        /// <param name="sender">Button initiating event.</param>
        /// <param name="e">RoutedEventArgs for button click</param>
        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            if (dgYourExpenseReport.SelectedIndex >= 0)
            {
                ((ExpenseData)ddsYourExpenseReports.DomainContext).SubmitExpenseReport((ExpenseReport)dgYourExpenseReport.SelectedItem);
                ddsYourExpenseReports.SubmitChanges();
            }
        }

        /// <summary>
        /// Approves the expense report and saves the changes to the database.
        /// </summary>
        /// <param name="sender">Button initiating event.</param>
        /// <param name="e">RoutedEventArgs for button click</param>
        private void ApproveButton_Click(object sender, RoutedEventArgs e)
        {
            if (dgApprovalExpenseReport.SelectedIndex >= 0)
            {
                ((ExpenseData)ddsYourExpenseReports.DomainContext).ApproveExpenseReport((ExpenseReport)dgYourExpenseReport.SelectedItem);
                ddsApprovalExpenseReports.SubmitChanges();
            }
        }

        /// <summary>
        /// Rejects the expense report and saves the changes to the database.
        /// </summary>
        /// <param name="sender">Button initiating event.</param>
        /// <param name="e">RoutedEventArgs for button click</param>
        private void RejectButton_Click(object sender, RoutedEventArgs e)
        {
            if (dgApprovalExpenseReport.SelectedIndex >= 0)
            {
                ((ExpenseData)ddsYourExpenseReports.DomainContext).RejectExpenseReport((ExpenseReport)dgYourExpenseReport.SelectedItem);
                ddsApprovalExpenseReports.SubmitChanges();
            }
        }

    }
}
